package utils

import (
	"fmt"

	"github.com/shirou/gopsutil/disk"
)

type DiskStatus struct {
	All  string `json:"all"`
	Used string `json:"used"`
	Free string `json:"free"`
}

// disk usage of path/disk
func DiskUsage(path string) (diskInfos DiskStatus) {

	parts, err := disk.Partitions(true)
	if err != nil {
		panic(err)
	}

	// for _, part := range parts {
	// 	diskItem, _ := disk.Usage(part.Mountpoint)
	// 	fmt.Printf("Disk: %s\n", part.Device)
	// 	fmt.Printf("\tTotal: %v GB\n", diskItem.Total/1024/1024/1024)
	// 	fmt.Printf("\tFree: %v GB\n", diskItem.Free/1024/1024/1024)
	// 	fmt.Printf("\tUsed: %v GB\n", diskItem.Used/1024/1024/1024)
	// }

	diskInfo, _ := disk.Usage(parts[0].Mountpoint)

	diskInfos.All = fmt.Sprintf("%.1f", float32(diskInfo.Total/1024/1024)/1024)

	diskInfos.Free = fmt.Sprintf("%.1f", float32(diskInfo.Free/1024/1024)/1024)
	diskInfos.Used = fmt.Sprintf("%.1f", float32(diskInfo.Used/1024/1024)/1024)

	// fs := syscall.Statfs_t{}
	// err := syscall.Statfs(path, &fs)
	// if err != nil {
	// 	return
	// }

	// fmt.Print(fs.Blocks * uint64(fs.Bsize))

	// diskInfos.All = fmt.Sprintf("%.1f", fs.Blocks*uint64(fs.Bsize))
	// diskInfos.Free = fmt.Sprintf("%.1f", fs.Bfree*uint64(fs.Bsize))
	// diskInfos.Used = fmt.Sprintf("%.1f", fs.Blocks*uint64(fs.Bsize)-fs.Bfree*uint64(fs.Bsize))

	return
}
